{% extends 'base.html' %}

{% block title %}管理员仪表板{% endblock %}

{% block content %}
<div class="row">
    <!-- 统计卡片 -->
    <div class="col-12 mb-4">
        <div class="row">
            <div class="col-md-2">
                <div class="stats-card text-center">
                    <h3>{{ stats.total_books }}</h3>
                    <p>总图书数</p>
                </div>
            </div>
            <div class="col-md-2">
                <div class="stats-card text-center">
                    <h3>{{ stats.total_users }}</h3>
                    <p>注册用户</p>
                </div>
            </div>
            <div class="col-md-2">
                <div class="stats-card text-center">
                    <h3>{{ stats.total_borrows }}</h3>
                    <p>总借阅数</p>
                </div>
            </div>
            <div class="col-md-2">
                <div class="stats-card text-center">
                    <h3>{{ stats.active_borrows }}</h3>
                    <p>当前借阅</p>
                </div>
            </div>
            <div class="col-md-2">
                <div class="stats-card text-center">
                    <h3>{{ stats.overdue_borrows }}</h3>
                    <p>逾期未还</p>
                </div>
            </div>
            <div class="col-md-2">
                <div class="stats-card text-center">
                    <h3>{{ stats.total_borrows|floatformat:1 }}</h3>
                    <p>平均借阅</p>
                </div>
            </div>
        </div>
    </div>

    <!-- 最近借阅记录 -->
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-clock me-2"></i>最近借阅记录</h5>
            </div>
            <div class="card-body">
                {% if recent_borrows %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>图书</th>
                                <th>借阅人</th>
                                <th>借阅日期</th>
                                <th>应还日期</th>
                                <th>状态</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in recent_borrows %}
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center">
                                        {% if record.book.cover_image %}
                                            <img src="{{ record.book.cover_image.url }}" class="rounded me-2" style="width: 40px; height: 50px; object-fit: cover;">
                                        {% else %}
                                            <div class="bg-light rounded me-2 d-flex align-items-center justify-content-center" style="width: 40px; height: 50px;">
                                                <i class="fas fa-book text-muted"></i>
                                            </div>
                                        {% endif %}
                                        <div>
                                            <h6 class="mb-0 small">{{ record.book.title }}</h6>
                                            <small class="text-muted">{{ record.book.author.name }}</small>
                                        </div>
                                    </div>
                                </td>
                                <td>{{ record.borrower.username }}</td>
                                <td>{{ record.borrow_date|date:"m-d H:i" }}</td>
                                <td>
                                    <span class="{% if record.is_overdue %}text-danger{% endif %}">
                                        {{ record.due_date|date:"m-d" }}
                                    </span>
                                </td>
                                <td>
                                    {% if record.is_overdue %}
                                        <span class="badge bg-danger">逾期</span>
                                    {% elif record.return_date %}
                                        <span class="badge bg-success">已还</span>
                                    {% else %}
                                        <span class="badge bg-primary">借阅中</span>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <p class="text-muted text-center py-3">暂无借阅记录</p>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- 侧边栏 -->
    <div class="col-lg-4">
        <!-- 热门图书 -->
        <div class="card mb-4">
            <div class="card-header">
                <h6 class="mb-0"><i class="fas fa-fire me-2"></i>热门图书</h6>
            </div>
            <div class="card-body">
                {% if popular_books %}
                    {% for book in popular_books %}
                    <div class="d-flex mb-3">
                        {% if book.cover_image %}
                            <img src="{{ book.cover_image.url }}" class="rounded me-3" style="width: 50px; height: 60px; object-fit: cover;">
                        {% else %}
                            <div class="bg-light rounded me-3 d-flex align-items-center justify-content-center" style="width: 50px; height: 60px;">
                                <i class="fas fa-book text-muted"></i>
                            </div>
                        {% endif %}
                        <div class="flex-grow-1">
                            <h6 class="mb-1">{{ book.title }}</h6>
                            <p class="text-muted small mb-1">{{ book.author.name }}</p>
                            <span class="badge bg-warning">{{ book.borrow_count }}次借阅</span>
                        </div>
                    </div>
                    {% endfor %}
                {% else %}
                <p class="text-muted text-center py-3">暂无数据</p>
                {% endif %}
            </div>
        </div>

        <!-- 逾期记录 -->
        <div class="card">
            <div class="card-header">
                <h6 class="mb-0"><i class="fas fa-exclamation-triangle me-2"></i>逾期记录</h6>
            </div>
            <div class="card-body">
                {% if overdue_records %}
                    {% for record in overdue_records %}
                    <div class="border-bottom pb-2 mb-2">
                        <h6 class="mb-1">{{ record.book.title }}</h6>
                        <p class="text-muted small mb-1">{{ record.borrower.username }}</p>
                        <span class="badge bg-danger">逾期{{ record.get_days_overdue }}天</span>
                    </div>
                    {% endfor %}
                {% else %}
                <p class="text-muted text-center py-3">暂无逾期记录</p>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 快速操作 -->
<div class="row mt-4">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-tools me-2"></i>快速操作</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-3 mb-3">
                        <a href="{% url 'add_book' %}" class="btn btn-primary w-100">
                            <i class="fas fa-plus me-2"></i>添加图书
                        </a>
                    </div>
                    <div class="col-md-3 mb-3">
                        <a href="{% url 'add_author' %}" class="btn btn-success w-100">
                            <i class="fas fa-user-plus me-2"></i>添加作者
                        </a>
                    </div>
                    <div class="col-md-3 mb-3">
                        <a href="{% url 'add_category' %}" class="btn btn-info w-100">
                            <i class="fas fa-tag me-2"></i>添加分类
                        </a>
                    </div>
                    <div class="col-md-3 mb-3">
                        <a href="{% url 'add_publisher' %}" class="btn btn-warning w-100">
                            <i class="fas fa-building me-2"></i>添加出版社
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 